package com.wcl.hencodertest.test07_drawing;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.Nullable;

import com.blankj.utilcode.util.ConvertUtils;

/**
 * @项目名： HenCoderTest
 * @包名： com.wcl.hencodertest.test07_drawing
 * @文件名: SportText
 * @创建者: kilin
 * @创建时间: 2020/4/17 11:18
 * @描述： TODO
 */
public class SportsText extends View {

    public static final int RADIUS = ConvertUtils.dp2px(150);
    private Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    private Rect rect = new Rect();
    public static final String TEXT = "abp测试";
    private Paint.FontMetrics fontMetrice = new Paint.FontMetrics();

    public SportsText(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    {
        paint.setTypeface(Typeface.createFromAsset(getContext().getAssets(), "Quicksand-Regular.ttf"));
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(ConvertUtils.dp2px(20));
        paint.setColor(Color.GRAY);
        canvas.drawCircle((float) getWidth() / 2, (float) getHeight() / 2, RADIUS, paint);

        paint.setColor(Color.RED);
        paint.setStrokeCap(Paint.Cap.ROUND);
        canvas.drawArc((float) getWidth() / 2 - RADIUS,
                (float) getHeight() / 2 - RADIUS,
                (float) getWidth() / 2 + RADIUS,
                (float) getHeight() / 2 + RADIUS,
                -135,
                200,
                false,
                paint);

        paint.setTextSize(200);
        paint.setStyle(Paint.Style.FILL);
        paint.getFontMetrics(fontMetrice);//文字会变,使用这个方式测量宽高,相对居中,缺点:不是绝对居中
        float offsetY = (fontMetrice.ascent + fontMetrice.descent) / 2F;
        paint.setTextAlign(Paint.Align.CENTER);
        canvas.drawText(TEXT, (float) getWidth() / 2, (float) getHeight() / 2 - offsetY, paint);


        paint.setTextAlign(Paint.Align.LEFT);
        paint.getTextBounds(TEXT, 0, TEXT.length(), rect);//文字不会变,使用这个方式测量宽高,绝对居中,缺点:文字改变可能会闪烁
        canvas.drawText(TEXT, -rect.left, -rect.top, paint);


    }
}
